package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

import model.Registration;
import model.Student;
import persistency.controller.RegistrationController;
import persistency.controller.StudentController;
import controller.PlanningController;
import controller.StartupController;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ScheduleFrame extends JFrame {
	/* Required for serializable interface */
	private static final long serialVersionUID = 1L;
	/* The controller for this view */
	private PlanningController controller = PlanningController.getInstance();
	/* List of students */
	private JComboBox<Student> cboStudent;
	/* Button to request schedule for given student */
	private JButton btnGetSchedule;
	/* Button to request schedule for all students */
	private JButton btnGetScheduleAll;
	/* Table to display the schedule */
	private JTable tblSchedule;
	private JButton jButtonUnregister;
	private JLabel jLabelMessage;
	private StartupController startupController;

	/** Constructs a new schedule frame */
	public ScheduleFrame(StartupController startupController) {
		this.setSize(800, 600);
		this.setLayout(new BorderLayout());
		this.init();
		this.loadStudents();
		this.getSchedule(null);
		//tblSchedule.getColumnModel().getColumn(5).setCellRenderer(new CWCheckBoxRenderer());
		this.startupController = startupController;
	}

	/* Builds the frame */
	private void init() {
		// Get content pane
		Container pane = this.getContentPane();

		// pnStudent
		JPanel pnStudent = new JPanel(new FlowLayout());

		// cboStudent
		this.cboStudent = new JComboBox<Student>();
		pnStudent.add(this.cboStudent);

		// btnGetSchedule
		this.btnGetSchedule = new JButton("Get");
		this.btnGetSchedule.addActionListener(new BtnGetScheduleClickedListener());
		pnStudent.add(this.btnGetSchedule);

		// btnGetScheduleAll
		this.btnGetScheduleAll = new JButton("Get all");
		this.btnGetScheduleAll.addActionListener(new BtnGetScheduleAllClickedListener());
		pnStudent.add(this.btnGetScheduleAll);
		/*{
			jButtonUnregister = new JButton();
			pnStudent.add(jButtonUnregister);
			jButtonUnregister.setText("Unregister");
			jButtonUnregister.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					
					Object registrationId = null;
					try {
						try {
							registrationId = tblSchedule.getModel().getValueAt(tblSchedule.getSelectedRow(), 6);
						} catch (Exception e) {
							throw new Exception("No value selected!");
						}
						
						RegistrationController.instance().deleteRegistration(registrationId);
						tblSchedule.repaint();
						jLabelMessage.setForeground(Color.GREEN);
						jLabelMessage.setText("Unregistration succesful");
					} catch (Exception e) {
						jLabelMessage.setForeground(Color.RED);
						jLabelMessage.setText("Unregistration failed " + e.getMessage());
						e.printStackTrace();
					}

				}
			});
		}*/

		// tblSchedule
		this.tblSchedule = new JTable();
		tblSchedule.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollPane = new JScrollPane(tblSchedule);
		
		
		
		// Add components to frame
		pane.add(pnStudent, BorderLayout.NORTH);
		pane.add(scrollPane, BorderLayout.SOUTH);
		{
			jLabelMessage = new JLabel();
			pane.add(jLabelMessage, BorderLayout.CENTER);
		}

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				showMenu();
			}
		});
		
	}

	/* Gets the schedule for a student or for all students */
	private void getSchedule(Student student) {
		Collection<Registration> schedule;

		// Get schedule
		if (student == null) {
			schedule = RegistrationController.instance().getAll();
		} else {
			schedule = RegistrationController.instance().getByStudentIdRegistrations(student.getStudentNumber());
		}

		// Set model for table
		ScheduleModel scheduleModel = new ScheduleModel(schedule);
		
		this.tblSchedule.setModel(scheduleModel);
		
	}

	/* Loads all student into the combo box */
	private void loadStudents() {
		// Clear the combobox
		this.cboStudent.removeAllItems();
		// Get a reference to the student container
		// Request a list of students
		ArrayList<Student> students = (ArrayList<Student>) StudentController.instance().getAll();
		// Add to combobox
		for (Student student : students) {
			this.cboStudent.addItem(student);
		}
	}

	/** Get schedule button click handler */
	public void BtnGetScheduleClickedHandler(ActionEvent e) {
		Student student = (Student) this.cboStudent.getSelectedItem();
		this.getSchedule(student);
	}

	/** Get schedule all button click handler */
	public void BtnGetScheduleAllClickedHandler(ActionEvent e) {
		this.getSchedule(null);
	}

	/** Get button click listener */
	private class BtnGetScheduleClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetScheduleClickedHandler(e);
		}
	}

	/** Get button click listener */
	private class BtnGetScheduleAllClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetScheduleAllClickedHandler(e);
		}
	}

	private void showMenu() {
		startupController.execute();
	}
	
	/*private class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
		protected JCheckBox checkBox;

		public CheckBoxCellEditor() {
			checkBox = new JCheckBox();
			checkBox.setHorizontalAlignment(SwingConstants.CENTER);
			checkBox.setBackground(Color.white);
		}

		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

			checkBox.setSelected(((Boolean) value).booleanValue());

			return checkBox;
		}

		public Object getCellEditorValue() {
			return Boolean.valueOf(checkBox.isSelected());
		}
	}*/
	
	private class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
		Border border = new EmptyBorder(1, 2, 1, 2);

		public CWCheckBoxRenderer() {
			super();
			setOpaque(true);
			setHorizontalAlignment(SwingConstants.CENTER);
		}

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

			if (value instanceof Boolean) {
				setSelected(((Boolean) value).booleanValue());
				setEnabled(table.isCellEditable(row, column));

				if (isSelected) {
					setBackground(table.getSelectionBackground());
					setForeground(table.getSelectionForeground());
				} else {
					setForeground(table.getForeground());
					setBackground(table.getBackground());
				}
			} else {
				// setSelected(false);
				// setEnabled(false);
				return null;
			}

			return this;
		}

	}
}